package com.ksd.modules.sysparameter.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.modules.sysparameter.entity.SysParameter;
import com.ksd.modules.sysparameter.mapper.SysParameterMapper;
import com.ksd.modules.sysparameter.po.SysParamsSearchPo;
import com.ksd.modules.sysparameter.service.ISysParameterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统参数 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-24
 */
@Service
public class SysParameterService extends ServiceImpl<SysParameterMapper, SysParameter> implements ISysParameterService {

    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;
    private final static String SYS_PARAM_KEY = "SYS_PARAM_VALUE:%s:%s";


    /**
     * 查找系统参数
     * @param sysParamsSearchPo
     * @return
     */
    @Override
    public List<SysParameter> findSysParameters(SysParamsSearchPo sysParamsSearchPo){
        LambdaQueryWrapper<SysParameter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysParameter::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(SysParameter::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(sysParamsSearchPo.getKeyword()),SysParameter::getParamName,sysParamsSearchPo.getKeyword());
        return  this.list(lambdaQueryWrapper);
    }


    @Override
    public SysParameter getByCode(String code) {
        LambdaQueryWrapper<SysParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysParameter::getParamCode, code);
        queryWrapper.eq(SysParameter::getSystemId, SystemIdThrealLocal.get().getSystemId());
        return this.getOne(queryWrapper);
    }

    @Override
    public <T> T getParam(String code, Class<T> classs) {
        String key = String.format(SYS_PARAM_KEY, SystemIdThrealLocal.get().getSystemId(),code);
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        if (map == null || map.isEmpty()) {
            if (redisTemplate.hasKey(key)) {
                return JSONUtil.toBean(JSON.toJSONString(map), classs);
            } else {
                SysParameter sysParameter = this.getByCode(code);
                if (sysParameter != null) {
                    String param = sysParameter.getParamValue();
                    Map<Object, Object> paramMap = JSONUtil.toBean(JSONUtil.parseObj(param, true), Map.class);
                    redisTemplate.opsForHash().putAll(key, paramMap);
                    return JSONUtil.toBean(param, classs);
                } else {
                    redisTemplate.opsForHash().putAll(key, Collections.emptyMap());
                    return JSONUtil.toBean(JSON.toJSONString(Collections.emptyMap()), classs);
                }
            }
        }
        return JSONUtil.toBean(JSON.toJSONString(map), classs);
    }

    @Override
    public void setParam(SysParameter sysParameter) {
        if (StringUtils.isBlank(sysParameter.getParamCode())) {
            return;
        }
        String key = String.format(SYS_PARAM_KEY,sysParameter.getSystemId(), sysParameter.getParamCode());
        if (JSONUtil.isTypeJSON(sysParameter.getParamValue())) {
            redisTemplate.opsForHash().putAll(key, JSONUtil.toBean(sysParameter.getParamValue(), Map.class));
        }
    }
}
